  $(document).ready(function () {
    if ($.cookie("rmbUser") == "true") {
    $("#rememberpass").attr("checked", true);
    $("#LAY-user-login-username").val($.cookie("username"));
    $("#LAY-user-login-password").val($.cookie("password"));
    }
  });

	//生成图形验证码,写在layui.use外面才有效
	var verifyCode = new GVerify("LAY-user-get-vercode");//用户名登录的图形验证码
	layui.use(['form','element','layer','jquery'],function(){
	//获取form表单
	var form = layui.form,
	element = layui.element,
	layer = layui.layer,
	$ = layui.$;
	
	
	//监听用户名登录提交，data为所监听对象的数据
	form.on('submit(LAY-user-login-submit)',function(obj){
		//获取表单的值
		var field = obj.field,
		username = field.username,					//用户名
		inputpass = field.password,					//密码
		vercode = field.vercode;					//图形验证码
		
		//校验图形验证码
		var res = verifyCode.validate(vercode);
		if(!res){
			layer.msg('图形验证码错误',{icon:5});
			$('#LAY-user-login-vercode').val("");
			return false;
		}
		
		//记住密码
		if ($("#rememberpass").attr("checked")) {
	      var str_username = $("#LAY-user-login-username").val();
	      var str_password = $("#LAY-user-login-password").val();
	      $.cookie("rmbUser", "true", { expires: 7 }); //存储一个带7天期限的cookie
	      $.cookie("username", str_username, { expires: 7 });
	      $.cookie("password", str_password, { expires: 7 });
	    }
	    else {
	      $.cookie("rmbUser", "false", { expire: -1 });
	      $.cookie("username", "", { expires: -1 });
	      $.cookie("password", "", { expires: -1 });
	    }
		
		//md5加密
	  	var salt = g_passsword_salt;
		var str = ""+salt.charAt(2)+salt.charAt(3)+salt.charAt(1)+inputpass + salt.charAt(6)+salt.charAt(7);
		var password = $.md5(str);
		//console.log(password);
		
		layer.load(2);//加载层
		//发送请求
		$.ajax({
			url:urlHead + "/user/doLogin"//服务器端地址
			,async:true
			,type:"POST"
			,data:{"username":username,"password":password,"src":4}
			,success:function(res)
			{
				console.log(res);

				if(res.code==0)
				{
					console.log(res.data.token);
					layer.closeAll('loading');//关闭加载层
					layer.msg('登录成功',
							{area:'200px',time:3000},
							function(){
								//存储token
								var date = new Date();
								date.setTime(date.getTime()+60*60*1000);//有效期为1小时
								$.cookie('access_token',res.data.token,{expires:date,path:'/',secure:false,raw:false});
								$.cookie('id',res.data.id,{expires:date,path:'/',secure:false,raw:false});
								$.cookie('telephone',res.data.telephone,{expires:date,path:'/',secure:false,raw:false});
								
								var token = $.cookie("access_token");
								console.log("Local Admin Token:"+token);
								//页面跳转
								if(res.data.roleType==111){//普通兑换上架员
									location.href ="../../views/adminframenormal.html?id="+res.data.id;//页面跳转的时候传递用户id
								}else if(res.data.roleType==100){//普通用户
									location.href ="../../views/exchange/beforelogin.html";
								}else if(res.data.roleType==112){//秒杀兑换上架员
									location.href ="../../views/adminframeseckill.html";
								}else{
									layer.msg('无权限在本平台登录',{icon:5});
								}
							});
				}else{
					layer.closeAll('loading');//关闭加载层
					layer.msg(res.msg,{icon:5});
				}
			}
			,error:function(){
				layer.closeAll('loading');//关闭加载层
				layer.msg('登录失败',{icon:5});
			}
			,dataType:"json"
		});
		
		return false;
	});
});